// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    text: ["公告","教学","交流"],
    message: [],
    isindex: true,
  },
  tabSelect(e) {
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    const db = wx.cloud.database();
    if(this.data.TabCur == 1){
      db.collection('messageInfo').limit(20).where({
        class : '教学'
      })
      .get().then(res => {
        res.data = this.formDate(res.data);
        let _message = []
        //console.log(res.data)
        res.data.forEach((i) =>{
          userInfo.classinfo.forEach((j =>{
            if(i.cId == j.cid){ 
              _message.push(i)
            }
          }))
        })
        this.setData({
          message: _message,
        });
        //console.log(this.data.message); 
      });
    }
    else if(this.data.TabCur == 2){
      db.collection('messageInfo').limit(20).where({
        class : '交流'
      })
      .get().then(res => {
        res.data = this.formDate(res.data);
        let _message = []
        res.data.forEach((i) =>{
          userInfo.classinfo.forEach((j =>{
            if(i.cId == j.cid){ 
              _message.push(i)
            }
          }))
        })
        this.setData({
          message: _message,
        });
      });
    }
    else{
      db.collection('messageInfo').limit(20).where({
        class : '公告'
      })
      .get().then(res => {
        res.data = this.formDate(res.data);
        let _message = []
        res.data.forEach((i) =>{
          userInfo.classinfo.forEach((j =>{
            if(i.cId == j.cid){ 
              _message.push(i)
            }
          }))
        })
        this.setData({
          message: _message,
        });
      });
    }
  },

  //搜索
  tosearch(){
    wx.navigateTo({
      url: '../index/search/search',
    })
  },

  formDate(data){
    for(let e of data){
      const year = e.date.getFullYear();
      const month = e.date.getMonth() + 1;
      const day = e.date.getDate();
      const date = year+"年"+month+"月"+day+"日";
      e.fdate = date;
    }
    return data;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const db = wx.cloud.database();
    let userInfo = wx.getStorageSync('userInfo');
    db.collection('messageInfo').limit(20).where({
      class : '公告',
    })
    .get().then(res => {
      res.data = this.formDate(res.data);
      let _message = []
      res.data.forEach((i) =>{
        userInfo.classinfo.forEach((j =>{
          if(i.cId == j.cid){ 
            _message.push(i)
          }
        }))
      })
      this.setData({
        message: _message,
      });
      // this.setData({
      //   message: _message,
      // });
      console.log(this.data.message); 
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
   
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: 0
    })
}
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})